import os
import asyncdispatch, asyncnet, terminal, strutils

proc interactiveClient(host: string, port: int,data: string) {.async.} =
  let socket = newAsyncSocket()
  try:
    await socket.connect(host, Port(port))
    echo "已连接服务器 ", host, ":", port
    echo "输入内容发送(CTRL+D退出)"
    
    while true:
      await socket.send(data & "\c\l")
      
      let response = await socket.recvLine()
      stdout.write "服务器响应: "
      stdout.styledWriteLine fgGreen, response
      
  except:
    stderr.writeLine "连接失败: ", getCurrentExceptionMsg()
  finally:
    socket.close()

when isMainModule:
  let params = commandLineParams()
  echo "使用方法: ./client.exe <IP> <端口> <数据>"
  echo "示例: ./client.exe 192.168.1.100 8888"
  echo "使用完毕，请使用ctrl+c退出并发送"
  if len(params) > 0:
    waitFor interactiveClient(params[0], params[1].parseInt,params[2])

